package com.noah.project.controller

import com.noah.project.dao.CareerDao
import com.noah.project.global.Const
import com.noah.project.global.JsonManager
import com.noah.project.global.TokenManager
import com.noah.project.model.Career
import com.noah.project.model.Company
import com.noah.project.model.Reply
import org.slf4j.LoggerFactory
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController

@RestController
class CareerApiController {
    @Autowired
    lateinit var careerDao: CareerDao
    val log = LoggerFactory.getLogger(CareerApiController::class.simpleName)

    /*****************项目******************/
    @RequestMapping(Const.API_ADD_CAREER)
    fun addCareer(company: Career, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (careerDao.findCareer(company.caid) != null) {
                return Reply(code = Const.ERROR_ALREADY, msg = "已经创建过的项目")
            }
            if (careerDao.insert(company) > 0) {
                return Reply(code = Const.SUCCESS, msg = "创建项目成功", data = JsonManager.toJson(company))
            }
            return Reply(code = Const.FAIL, msg = "创建项目失败")
        } catch (e: Exception) {
            log.error("创建项目", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "项目异常")
        }
    }

    @RequestMapping(Const.API_DELETE_CAREER)
    fun deleteCareer(career: Career, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            if (career.caid != 0L) {
                if (careerDao.findCareer(career.caid) == null) {
                    return Reply(code = Const.ERROR_ALREADY, msg = "没有项目可以删除")
                }
                if (careerDao.delete(career.caid) > 0) {
                    return Reply(code = Const.SUCCESS, msg = "删除项目成功")
                }
            } else if (career.aid != 0L) {
                if (careerDao.findCareers(career.aid).isEmpty()) {
                    return Reply(code = Const.ERROR_ALREADY, msg = "没有项目可以删除")
                }
                if (careerDao.deleteAll(career.aid) > 0) {
                    return Reply(code = Const.SUCCESS, msg = "删除项目成功")
                }
            }
            return Reply(code = Const.FAIL, msg = "删除项目失败")
        } catch (e: Exception) {
            log.error("删除项目", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "项目异常")
        }
    }

    @RequestMapping(Const.API_UPDATE_CAREER)
    fun updateCareer(career: Career, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val ret = careerDao.update(career)
            if (ret > 0) {
                val career = careerDao.findCareer(career.caid)
                return Reply(code = Const.SUCCESS, msg = "修改成功", data = JsonManager.toJson(career))
            }
            return Reply(code = Const.FAIL, msg = "修改失败")
        } catch (e: Exception) {
            log.error("项目公司", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "项目异常")
        }
    }

    @RequestMapping(Const.API_GET_CAREER_LIST)
    fun getCareerList(aid: Long): Reply {
        try {
            val careers = careerDao.findCareers(aid)
            if (careers.isNotEmpty()) {
                return Reply(code = Const.SUCCESS, msg = "获取项目列表成功", data = JsonManager.toJson(careers))
            }
            return Reply(code = Const.FAIL, msg = "没有更多项目")
        } catch (e: Exception) {
            log.error("项目列表", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "项目异常")
        }
    }

    @RequestMapping(Const.API_GET_CAREER)
    fun getCareer(caid: Long, token: String?): Reply {
        try {
            if (!TokenManager.checkToken(token)) {
                return Reply(code = Const.FAIL_TOKEN, msg = "Token失效")
            }
            val career =  careerDao.findCareer(caid)
            if (career != null) {
                return Reply(code = Const.SUCCESS, msg = "获取项目成功", data = JsonManager.toJson(career))
            } else {
                return Reply(code = Const.FAIL, msg = "获取项目失败")
            }
        } catch (e: Exception) {
            log.error("项目公司", e)
            return Reply(code = Const.FAIL_REQUEST, msg = "项目异常")
        }
    }
}